package cn.javasea.rbac.shiro.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

/**
 * @Description: 用户认证和授权
 * @Author: xiangchao
 * @Date: 2023/2/1
 */
public class ShiroRealm extends AuthorizingRealm {

    private static final Logger log = LoggerFactory.getLogger(ShiroRealm.class);

//    @Resource
//    private UserMapper userMapper;
//    @Resource
//    private RoleMapper roleMapper;
//    @Resource
//    private MenuMapper menuMapper;

    /**
     * 授权 用户第一次登录成功，请求接口的时候开始授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        log.info("开始执行授权操作.......");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        Set<String> userPerms = new HashSet<>();
        userPerms.add("sss:ss");
        userPerms.add("sss:xx");
        info.setStringPermissions(userPerms);
        return info;
    }

    /**
     * 登录认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        log.info("开始进行身份认证......");
        User user = new User();
        user.setPassword("admin123");
        user.setUsername("admin");
        user.setId(9L);
        return new SimpleAuthenticationInfo(user, user.getPassword(), getName());
    }

}
